﻿using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace BlazorAdmin.Web.Shared.Filter
{
    /// <summary>
    /// 认证头部参数
    /// </summary>
    public class AuthTokenHeaderParameter : IOperationFilter
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="operation"></param>
        /// <param name="context"></param>
        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            if (null == operation.Parameters)
                operation.Parameters = new List<OpenApiParameter>();
            var attrs = context.ApiDescription.CustomAttributes();
            foreach (var attr in attrs)
            {
                if (attr.GetType() != typeof(AuthToken))
                    continue;

                operation.Parameters.Add(new OpenApiParameter
                {
                    Name = "Authorization",
                    In = ParameterLocation.Header,
                    Description = "用户Token",
                    Required = true
                });
            }
        }
    }
}
